package com.csun.cmny.provider.controller;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.pojo.CacBaseBigItem;
import com.csun.cmny.provider.pojo.CacBaseMinorItem;
import com.csun.cmny.provider.service.CacBaseBigItemService;
import com.csun.cmny.provider.service.CacBaseMinorItemService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/*
 * 评估大项管理——基础大项
 * */
@RestController
@RequestMapping("/CacBaseBigItem")
public class CacBaseBigItemController extends BaseController {
    @Autowired
    private CacBaseBigItemService cacBaseBigItemService;
    @Autowired
    private CacBaseMinorItemService cacBaseMinorItemService;

    /*
     * 这里的认证和当前登录的用户注解先预留，等完善了再进行修改
     * */
    //添加单个大项
    @RequestMapping(value = "/addCacBaseBigItem", method = RequestMethod.POST)
    public Wrapper<CacBaseBigItem> addCacBaseBigItem(CacBaseBigItem cacBaseBigItem) {
        //校验
        if (cacBaseBigItem.getName() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "大项名称不能为空", null);
        }
        if (cacBaseBigItem.getCacTypeId() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "类别id不能为空", null);
        }
        if (cacBaseBigItem.getType() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "类别不能为空", null);
        }
        cacBaseBigItem.setGroupId(getLoginAuthDto().getGroupId());//增加机构ID
        CacBaseBigItem cacBaseBigItem1 = new CacBaseBigItem();
        cacBaseBigItem1.setCacTypeId(cacBaseBigItem.getCacTypeId());
        cacBaseBigItem1.setName(cacBaseBigItem.getName());
        cacBaseBigItem1.setGroupId(getLoginAuthDto().getGroupId());//增加机构ID

        //根据大项名称、类别id和类别判断该大项是否在该类别中已经存在
        CacBaseBigItem cacBaseBigItem2 = cacBaseBigItemService.selectByCbi(cacBaseBigItem1);
        if (cacBaseBigItem2 != null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "该大项已经在该类别中存在", null);
        }
        if (cacBaseBigItem.getType() == 1 || cacBaseBigItem.getType() == 2) {
            CacBaseBigItem cacBaseBigItem3 = new CacBaseBigItem();
            cacBaseBigItem3.setCacTypeId(cacBaseBigItem.getCacTypeId());
            cacBaseBigItem3.setType(cacBaseBigItem.getType());
            cacBaseBigItem3.setGroupId(getLoginAuthDto().getGroupId());//增加机构ID
            //判断类别下只能存在一个等级项或总分项
            CacBaseBigItem cacBaseBigItem4 = cacBaseBigItemService.selectByCbi(cacBaseBigItem3);
            if (cacBaseBigItem4 != null) {
                return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "该类别下只能存在一个等级项或总分项", null);
            }
        }

        int n = cacBaseBigItemService.insert(cacBaseBigItem);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "添加失败", cacBaseBigItem);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "添加成功", cacBaseBigItem);
    }

    //添加单个大项——批量新增小项
    @RequestMapping(value = "/addCacBaseBigItems", method = RequestMethod.POST)
    public Wrapper addCacBaseBigItems(@RequestBody CacBaseBigItem cacBaseBigItem) {
        //校验
        if (cacBaseBigItem.getName() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "大项名称不能为空", null);
        }
        if (cacBaseBigItem.getCacTypeId() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "类别id不能为空", null);
        }
        if (cacBaseBigItem.getType() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "类别不能为空", null);
        }
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Long modifier = loginAuthDto.getUserId();
        cacBaseBigItem.setGroupId(getLoginAuthDto().getGroupId());//添加组织ID

        CacBaseBigItem cacBaseBigItem1 = new CacBaseBigItem();
        cacBaseBigItem1.setCacTypeId(cacBaseBigItem.getCacTypeId());
        cacBaseBigItem1.setName(cacBaseBigItem.getName());
        cacBaseBigItem1.setGroupId(getLoginAuthDto().getGroupId());//添加组织ID
        //根据大项名称、类别id和类别判断该大项是否在该类别中已经存在
        CacBaseBigItem cacBaseBigItem2 = cacBaseBigItemService.selectByCbi(cacBaseBigItem1);
        if (cacBaseBigItem2 != null) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "该大项已经在该类别中存在", null);
        }
        if (cacBaseBigItem.getType() == 1 || cacBaseBigItem.getType() == 2) {
            CacBaseBigItem cacBaseBigItem3 = new CacBaseBigItem();
            cacBaseBigItem3.setCacTypeId(cacBaseBigItem.getCacTypeId());
            cacBaseBigItem3.setType(cacBaseBigItem.getType());
            cacBaseBigItem3.setGroupId(getLoginAuthDto().getGroupId());//添加组织ID
            //判断类别下只能存在一个等级项或总分项
            CacBaseBigItem cacBaseBigItem4 = cacBaseBigItemService.selectByCbi(cacBaseBigItem3);
            if (cacBaseBigItem4 != null) {
                return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "该类别下只能存在一个等级项或总分项", null);
            }
        }
        cacBaseBigItem.setModifier(modifier);
        int n = cacBaseBigItemService.insert(cacBaseBigItem);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "添加失败", cacBaseBigItem);
        }
        //批量新增小项
        List<CacBaseMinorItem> cacBaseMinorItemList = cacBaseBigItem.getCacBaseMinorItemList();
        if (!CollectionUtils.isEmpty(cacBaseMinorItemList)) {
            //校验
            for (int i = 0; i < cacBaseMinorItemList.size(); i++) {
                if (org.springframework.util.StringUtils.isEmpty(cacBaseMinorItemList.get(i).getName())) {
                    return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "小项类别名称不能为空", cacBaseMinorItemList.get(i));
                }
                if (cacBaseBigItem.getType() == 0 && cacBaseMinorItemList.get(i).getGrade() == null) {
                    return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "小项分数不能为空", cacBaseMinorItemList.get(i));
                }
                for (int j = 0; j < cacBaseMinorItemList.size(); j++) {
                    if (j != i && cacBaseMinorItemList.get(i).getName().equals(cacBaseMinorItemList.get(j).getName())) {
                        return WrapMapper.wrap(Wrapper.ERROR_CODE, "小项类别名称不能相同", null);
                    }
                    cacBaseMinorItemList.get(i).setBigItemId(cacBaseBigItem.getBigItemId());
                    cacBaseMinorItemList.get(i).setModifier(modifier);
                    cacBaseMinorItemList.get(i).setGroupId(getLoginAuthDto().getGroupId());
                }
            }
            //批量插入
            int m = cacBaseMinorItemService.insertBatch(cacBaseMinorItemList);
            if (m <= 0) {
                System.out.println("********************新增大项时批量插入小项失败********************");
            }
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "添加成功", cacBaseBigItem);
    }

    //根据大项id删除大项及大项下的小项
    @RequestMapping(value = "/delCacBaseBigItem", method = RequestMethod.POST)
    public Wrapper delCacBaseBigItem(Integer bigItemId) {
        if (bigItemId == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "大项id不能为空", null);
        }
        //删除大项下面的小项
        int m = cacBaseMinorItemService.delByBid(bigItemId);
        //删除大项
        int n = cacBaseBigItemService.delById(bigItemId);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "删除失败", n);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "删除成功", n);
    }

    //修改单个大项
    @RequestMapping(value = "/updateCacBaseBigItem", method = RequestMethod.POST)
    public Wrapper<CacBaseBigItem> updateCacBaseBigItem(CacBaseBigItem cacBaseBigItem) {
        if (cacBaseBigItem.getBigItemId() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "大项id不能为空", null);
        }
        cacBaseBigItem.setGroupId(getLoginAuthDto().getGroupId());//添加组织ID
        CacBaseBigItem cacBaseBigItem1 = new CacBaseBigItem();
        cacBaseBigItem1.setCacTypeId(cacBaseBigItem.getCacTypeId());
        cacBaseBigItem1.setName(cacBaseBigItem.getName());
        cacBaseBigItem1.setGroupId(getLoginAuthDto().getGroupId());//添加组织ID
        //根据大项名称和类别id判断该大项是否在该大项中已经存在
        CacBaseBigItem cacBaseBigItem2 = cacBaseBigItemService.selectByCbi(cacBaseBigItem1);
        if (cacBaseBigItem2 != null && !cacBaseBigItem2.getBigItemId().equals(cacBaseBigItem.getBigItemId())) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "该大项已经在该类别中存在", cacBaseBigItem2);
        }
        if (cacBaseBigItem.getType() == 1 || cacBaseBigItem.getType() == 2) {
            CacBaseBigItem cacBaseBigItem3 = new CacBaseBigItem();
            cacBaseBigItem3.setCacTypeId(cacBaseBigItem.getCacTypeId());
            cacBaseBigItem3.setType(cacBaseBigItem.getType());
            cacBaseBigItem3.setGroupId(getLoginAuthDto().getGroupId());//添加组织ID
            //判断类别下只能存在一个等级项或总分项
            CacBaseBigItem cacBaseBigItem4 = cacBaseBigItemService.selectByCbi(cacBaseBigItem3);
            if (cacBaseBigItem4 != null) {
//                if (!(cacBaseBigItem4.getName().equals(cacBaseBigItem.getName()) && cacBaseBigItem4.getInstruction().equals(cacBaseBigItem.getInstruction()))){
//                    return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"该类别下只能存在一个等级项或总分项",null);
//                }
                if (!cacBaseBigItem.getBigItemId().equals(cacBaseBigItem4.getBigItemId())) {
                    return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "该类别下只能存在一个等级项或总分项", null);
                }
            }
        }
        int n = cacBaseBigItemService.update(cacBaseBigItem);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "修改失败", cacBaseBigItem);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "修改成功", cacBaseBigItem);
    }

    //根据大项id查询单个大项
    @RequestMapping(value = "/selectBaseCbiById", method = RequestMethod.POST)
    public Wrapper selectBaseCbiById(Integer bigItemId) {
        if (bigItemId == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "大项id不能为空", null);
        }
        CacBaseBigItem cacBaseBigItem = cacBaseBigItemService.selectCbiById(bigItemId);
        if (cacBaseBigItem == null) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "没有此id", bigItemId);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "查询成功", cacBaseBigItem);
    }

    //根据大项id查询单个大项及大项下的小项
    @RequestMapping(value = "/selectOneById", method = RequestMethod.POST)
    public Wrapper selectOneById(Integer bigItemId) {
        if (bigItemId == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "大项id不能为空", null);
        }
        CacBaseBigItem cacBaseBigItem = cacBaseBigItemService.selectOneById(bigItemId);
        if (cacBaseBigItem == null) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "没有此id", bigItemId);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "查询成功", cacBaseBigItem);
    }
}
